package me.zhengjie.modules.web.service.chat.validate.impl;

import cn.hutool.core.util.ObjectUtil;
import me.zhengjie.modules.system.service.UserChatPriceSettingService;
import me.zhengjie.modules.system.service.dto.UserChatPriceSettingDto;
import me.zhengjie.modules.system.service.dto.UserChatPriceSettingQueryCriteria;
import me.zhengjie.modules.web.domain.chat.BaiduChatResponse;
import me.zhengjie.modules.web.service.chat.validate.IReplayFeeValidate;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 每日免费次数
 */
@Service("FreeModelValidate")
@Order(2)
public class FreeModelValidate implements IReplayFeeValidate {

    @Autowired
    UserChatPriceSettingService userChatPriceSettingService;

    @Resource
    RedisUtils redisUtils;

    @Override
    public boolean validate(String openId, BaiduChatResponse response) {
        UserChatPriceSettingQueryCriteria criteria = new UserChatPriceSettingQueryCriteria();
        criteria.setSettingStatus(1);
        List<UserChatPriceSettingDto> settingList = userChatPriceSettingService.queryAll(criteria);
        if(CollectionUtils.isEmpty(settingList)){
            return false;
        }
        UserChatPriceSettingDto settingDto =  settingList.stream().filter(s -> "FreeModel".equals(s.getSettingKey())).findFirst().orElseGet(() -> null);
        if(ObjectUtil.isEmpty(settingDto) || ObjectUtil.isEmpty(settingDto.getSettingValue())){
            return false;
        }
        String keyDec = "replay:validate:"+openId+":"+ DateUtil.getNow();
        redisUtils.setIfAbsent(keyDec,Integer.parseInt(settingDto.getSettingValue()),1L, TimeUnit.DAYS);
        long surplus = redisUtils.decr(keyDec);
        boolean success = false;
        if(surplus<0){
            success = false;
            surplus = 0L;
        } else {
            success = true;
        }
        response.setSurplusTimes(surplus);
        response.setSurplusUnit(0);
        return success;
    }

    @Override
    public boolean get(String openId, BaiduChatResponse response) {
        UserChatPriceSettingQueryCriteria criteria = new UserChatPriceSettingQueryCriteria();
        criteria.setSettingStatus(1);
        List<UserChatPriceSettingDto> settingList = userChatPriceSettingService.queryAll(criteria);
        if(CollectionUtils.isEmpty(settingList)){
            return false;
        }
        UserChatPriceSettingDto settingDto =  settingList.stream().filter(s -> "FreeModel".equals(s.getSettingKey())).findFirst().orElseGet(() -> null);
        if(ObjectUtil.isEmpty(settingDto) || ObjectUtil.isEmpty(settingDto.getSettingValue())){
            return false;
        }
        String keyDec = "replay:validate:"+openId+":"+ DateUtil.getNow();
        redisUtils.setIfAbsent(keyDec,Integer.parseInt(settingDto.getSettingValue()),1L, TimeUnit.DAYS);
        Object value = redisUtils.get(keyDec);
        long surplus = ObjectUtil.isEmpty(value)?0:Long.parseLong(value.toString());
        boolean success = false;
        if(surplus<=0){
            success = false;
            surplus = 0L;
        } else {
            success = true;
        }
        response.setSurplusTimes(surplus);
        response.setSurplusUnit(0);
        return success;
    }
}
